#include <stdio.h>
#include "gmp.h"

int main(int argc, char* argv[])
{
	mpz_t num1, num2;
	char input_str[1000];
	int x;
	unsigned long int y;
	FILE *stream;
	
	mpz_init(num1);
	mpz_init(num2);
	y = 2;
	stream = fopen("primesLessThan.txt", "w");

	// No number given yet
	if(argc == 1){
		printf("Type a number: ");
		gets(input_str);
		mpz_set_str(num1, input_str, 10);
	} else {
		mpz_set_str(num1, argv[1], 10);
	}

	gmp_printf("\n{ ");	
	x = mpz_cmp_ui(num1, y);


	// check if given number is grater than 2
	if(x > 0){
		// print first prime
		mpz_nextprime(num2, num2);
		gmp_printf("%Zd", num2);
		mpz_out_str(stream, 10, num2);
		fprintf(stream, "\n");
		mpz_nextprime(num2, num2);
		x = mpz_cmp(num2, num1);

		// print the rest primes with comma ',' before
		while (x < 0){
			gmp_printf(", %Zd", num2);
			mpz_out_str(stream, 10, num2);
			fprintf(stream, "\n");
			mpz_nextprime(num2, num2);
			x = mpz_cmp(num2, num1);
		}
		
	}
	

	gmp_printf(" }\n");
	fclose(stream);

	mpz_clear(num1);
	mpz_clear(num2);
	return 0;
}
